package com.chart.web.controller.chart.sta;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.chart.chart.domian.ChartConfigs;
import com.chart.chart.domian.ChartLegendData;
import com.chart.chart.domian.dto.ChartDateTto;
import com.chart.chart.domian.utils.ChartValidator;
import com.chart.chart.domian.vo.ChartSmartVo;
import com.chart.chart.domian.vo.ChartXDataVo;
import com.chart.chart.service.IChartConfigsService;
import com.chart.common.core.controller.BaseController;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.utils.ChartDataUtils;
import com.chart.common.utils.ColorUtils;
import com.chart.common.utils.ExcelDataUtils;
import com.chart.common.utils.ExcelUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static com.chart.web.controller.chart.sta.JsonsUtil.updateLegendDataNames;


/**
 * 3D柱状图
 *
 * @author chart
 */
@RestController
@RequestMapping("/bar3D/chart")
public class Bar3DChartController extends BaseController {

    @Autowired
    private IChartConfigsService chartConfigsService;


    //多排3D柱状图
    @PostMapping("/multi3DBar/importData")
    public AjaxResult importData(MultipartFile file, Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取excel读取到的数据
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xList = new ArrayList<>();
            List<String> yList = new ArrayList<>();


            //处理数据
            List<List<String>> dataList = new ArrayList<>();
            for (int i = 0; i < getdata.size(); i++) {
                List<String> ss = getdata.get(i);
                if (!CollectionUtils.isEmpty(ss) && !ss.equals("")) {
                    if (i == 0) {
                        for (int i1 = 1; i1 < ss.size(); i1++) {
                            String y = ss.get(i1);
                            //防止出现为空值或为空字符的数值
                            if (!ObjectUtils.isEmpty(y) && !y.equals("")) {
                                yList.add(y);
                            }
                        }
                    } else {
                        String x = ss.get(0);
                        if (!ObjectUtils.isEmpty(x) && !x.equals("")) {
                            xList.add(x);
                        }
                        getdata.get(i).remove(0);
                        dataList.add(getdata.get(i));
                    }
                }
            }


//            yList.remove(0);

            List<ChartXDataVo> xData = new ArrayList<>();

            for (int i = 0; i < yList.size(); i++) {
                String y = yList.get(i);
                List<List<Object>> newDataList = new ArrayList<>();
                for (int j = 0; j < xList.size(); j++) {
                    String s = dataList.get(j).get(i);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        String x = xList.get(j);
                        List<Object> cellList = new ArrayList<>();
                        cellList.add(x);
                        cellList.add(y);
                        cellList.add(Double.parseDouble(s));
                        newDataList.add(cellList);
                    }
                }
                ChartXDataVo chartXDataVo = new ChartXDataVo();
                chartXDataVo.setSource(newDataList);
                xData.add(chartXDataVo);
            }


            //获取到对应图表的配置，将它反序列化
            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            jsonObject.put("dataset", xData);

            //获取图例配置
            JSONObject legend = (JSONObject) jsonObject.get("legend");
            List<ChartLegendData> chartLegendData = new ArrayList<>();

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject o = series.get(0);
            List<JSONObject> seriesList = new ArrayList<>();
            for (int i = 0; i < yList.size(); i++) {
                JSONObject o1 = new JSONObject();
                o1.putAll(o);
                o1.put("datasetIndex", i);
                o1.put("name", yList.get(i));
                seriesList.add(o1);

                ChartLegendData data = new ChartLegendData();
                data.setName(yList.get(i));
                chartLegendData.add(data);
            }
            jsonObject.put("series", seriesList);

            legend.put("data", chartLegendData);
            jsonObject.put("legend", legend);

            JSONObject grid3D = (JSONObject) jsonObject.get("grid3D");

            //根据数据多少将x,y轴进行按比例放大
            if (xList.size() > 10 || yList.size() > 10) {
                grid3D.put("boxWidth", xList.size() * 8);
                grid3D.put("boxDepth", yList.size() * 8);

            } else if (xList.size() <= 10 && yList.size() <= 10) {
                grid3D.put("boxWidth", xList.size() * 20);
                grid3D.put("boxDepth", yList.size() * 20);
            }

            jsonObject.put("grid3D", grid3D);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
            workbook.close();
        }
    }
/*    @PostMapping("/multi3DSectionBar/importData")
    public AjaxResult multi3DSectionBar(@RequestBody ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        Integer configId = chartDateTto.getConfigId();
        List<List<String>> getdata = extractDataFromListNullString(chartDateTto.getDataList());

        // 获取 seriesName 列表
        ArrayList<String> seriesName = new ArrayList<>();
        for (int rowIndex = 0; rowIndex < getdata.size(); rowIndex++) {
            List<String> row = getdata.get(rowIndex);
            String s = row.get(0);
            seriesName.add(s);
            for (int colIndex = 0; colIndex < row.size(); colIndex++) {
                String cellValue = row.get(colIndex);
                System.out.println("第 " + (colIndex + 1) + " 列: " + cellValue);
            }
        }
        seriesName.remove(0);

        // 计算 size 为 seriesName 的大小
        int size = getdata.size();

        // 处理颜色字段
        ObjectNode updatedJson = ColorUtils.processChartColors(chartValue, size);

        // 更新 series
        updatedJson = JsonsUtil.updateSeriesInJson(updatedJson.toString(), seriesName);

        // 更新 legend 中 data 数组的 name 字段
        updateLegendDataNames(updatedJson, seriesName);

        // 生成并更新 dataset
        ArrayNode datasetArray = JsonsUtil.generateDataset(getdata);
        updatedJson.set("dataset", datasetArray);

        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigValue(JSON.toJSONString(updatedJson));
        newList.add(chartSmartVo);

        System.out.println(JSON.toJSONString(updatedJson));
        return AjaxResult.success(newList);
    }*/


/*    @PostMapping("/multi3DSectionBar/importData")
    public AjaxResult multi3DSectionBar(@RequestBody ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        Integer configId = chartDateTto.getConfigId();
        List<List<String>> getdata = extractDataFromListNullString(chartDateTto.getDataList());

        // 获取 seriesName 列表
        ArrayList<String> seriesName = new ArrayList<>();

        // 遍历 Excel 的每一行数据
        for (int rowIndex = 0; rowIndex < getdata.size(); rowIndex++) {
            List<String> row = getdata.get(rowIndex);
            String s = row.get(0); // 第一行第一列的数据放入集合中
            seriesName.add(s);
            // 遍历当前行中的每一列
            for (int colIndex = 0; colIndex < row.size(); colIndex++) {
                String cellValue = row.get(colIndex);
                System.out.println("第 " + (colIndex + 1) + " 列: " + cellValue);
            }
        }
        seriesName.remove(0);

        // 计算 size 为 seriesName 的大小
        int size = getdata.size();
        // 初始化 ObjectMapper 和 JsonNode
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(chartValue);
        // 获取 color 字段
        JsonNode colorNode = jsonNode.path("color");
        // 创建一个 List<String> 来存放 color 字段值
        List<String> baseColors = new ArrayList<>();

        // 遍历 color 数组并将每个颜色值添加到 baseColors 集合中
        if (colorNode.isArray()) {
            for (JsonNode color : colorNode) {
                String colorValue = color.asText();
                baseColors.add(colorValue);
            }
        }
        // 输出 color 字段的值
        System.out.println("我是color字段: " + baseColors);
        // 生成渐变颜色列表
        List<String> gradientColors = ColorUtils.generateGradientColors(baseColors, size);
        // 用 gradientColors 替换原来的 color 字段
        ArrayNode gradientColorsNode = objectMapper.valueToTree(gradientColors);
        jsonNode.set("color", gradientColorsNode);

        // 更新 series
        ObjectNode updatedJson = JsonsUtil.updateSeriesInJson(jsonNode.toString(), seriesName);

        // 更新 legend 中 data 数组的 name 字段
        updateLegendDataNames(updatedJson, seriesName);

        // 生成并更新 dataset
        ArrayNode datasetArray = JsonsUtil.generateDataset(getdata);
        updatedJson.set("dataset", datasetArray);

        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigValue(JSON.toJSONString(updatedJson));
        newList.add(chartSmartVo);

        System.out.println(JSON.toJSONString(updatedJson));
        return AjaxResult.success(newList);
    }*/





  /*  @PostMapping("/multi3DSectionBar/importData")
    public AjaxResult multi3DSectionBar(@RequestBody ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        Integer configId = chartDateTto.getConfigId();
        List<List<String>> getdata = extractDataFromListNullString(chartDateTto.getDataList());


        // 获取seriesName列表
        ArrayList<String> seriesName = new ArrayList<>();

        //遍历excel的每一行数据
        for (int rowIndex = 0; rowIndex < getdata.size(); rowIndex++) {
            List<String> row = getdata.get(rowIndex);
            String s = row.get(0);//第一行第一列的数据放入集合中
            seriesName.add(s);
            // 遍历当前行中的每一列
            for (int colIndex = 0; colIndex < row.size(); colIndex++) {
                String cellValue = row.get(colIndex);
                System.out.println("  第 " + (colIndex + 1) + " 列: " + cellValue);

            }
        }
        seriesName.remove(0);

        int size = seriesName.size();


        // 初始化 ObjectMapper 和 JsonNode
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(chartValue);

        // 获取 color 字段
        JsonNode colorNode = jsonNode.path("color");

        // 创建一个 List<String> 来存放 color 字段值
        List<String> baseColors = new ArrayList<>();

        // 遍历 color 数组并将每个颜色值添加到 baseColors 集合中
        if (colorNode.isArray()) {
            for (JsonNode color : colorNode) {
                String colorValue = color.asText();
                baseColors.add(colorValue);
            }
        }

        // 输出 color 字段的值
        System.out.println("我是color字段: " + baseColors);

            List<String> gradientColors = ColorUtils.generateGradientColors(baseColors, size);



        // 更新series
        ObjectNode updatedJson = JsonsUtil.updateSeriesInJson(chartValue, seriesName);
        // 更新 legend 中 data 数组的 name 字段
        updateLegendDataNames(updatedJson, seriesName);



        // 生成并更新 dataset
        ArrayNode datasetArray = JsonsUtil.generateDataset(getdata);
        updatedJson.set("dataset", datasetArray);

        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigValue(JSON.toJSONString(updatedJson));
        newList.add(chartSmartVo);

        System.out.println(JSON.toJSONString(updatedJson));
        return AjaxResult.success(newList);

    }*/



/*
    @PostMapping("/multi3DSectionBar/importData")
    public AjaxResult multi3DSectionBar(@RequestBody ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        Integer configId = chartDateTto.getConfigId();
        List<List<String>> getdata = extractDataFromListNullString(chartDateTto.getDataList());


        // 获取seriesName列表
        ArrayList<String> seriesName = new ArrayList<>();

        //遍历excel的每一行数据
        for (int rowIndex = 0; rowIndex < getdata.size(); rowIndex++) {
            List<String> row = getdata.get(rowIndex);
            String s = row.get(0);//第一行第一列的数据放入集合中
            seriesName.add(s);
            // 遍历当前行中的每一列
            for (int colIndex = 0; colIndex < row.size(); colIndex++) {
                String cellValue = row.get(colIndex);
                System.out.println("  第 " + (colIndex + 1) + " 列: " + cellValue);

            }
        }
        seriesName.remove(0);



        // 更新series
        ObjectNode updatedJson = JsonsUtil.updateSeriesInJson(chartValue, seriesName);


        // 更新 legend 中 data 数组的 name 字段
        updateLegendDataNames(updatedJson, seriesName);
        // 生成并更新 dataset
        ArrayNode datasetArray = JsonsUtil.generateDataset(getdata);
        updatedJson.set("dataset", datasetArray);



        ObjectMapper objectMapper = new ObjectMapper();

       // 处理颜色字段
        ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(chartValue);
    // 获取 color 字段
        JsonNode colorNode = jsonNode.path("color");

        List<String> baseColors = new ArrayList<>();
       // 遍历 color 数组并将每个颜色值添加到 baseColors 集合中
        if (colorNode.isArray()) {
            for (JsonNode color : colorNode) {
                String colorValue = color.asText();
                baseColors.add(colorValue);
            }
        }

        int beforeSize = baseColors.size();
        int afterSize = lists.get(0).size();
        if(beforeSize<afterSize){
            chartValueJson = ColorUtils.processChartColorsHert(JSON.toJSONString(chartValue), lists.size());
        }



        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigValue(JSON.toJSONString(updatedJson));
        newList.add(chartSmartVo);
        System.out.println(JSON.toJSONString(updatedJson));
        return AjaxResult.success(newList);

    }*/


    /**
     * 可以了 好用的 多排 3D切面
     *
     * @param chartDateTto
     * @return
     * @throws Exception
     */
/*    @PostMapping("/multi3DSectionBar/importData")
    public AjaxResult multi3DSectionBar(@RequestBody ChartDateTto chartDateTto) throws Exception {
          // 参数校验
        String validationMessage = ChartValidator.check(chartDateTto);
        if (validationMessage != null) {
            // 返回校验失败的提示信息给前端
            return AjaxResult.error(validationMessage);
        }

        String chartValue = chartDateTto.getChartValue();
        Integer configId = chartDateTto.getConfigId();
        List<List<String>> getdata = extractDataFromListNullString(chartDateTto.getDataList());

        // 获取seriesName列表
        ArrayList<String> seriesName = new ArrayList<>();

        //遍历excel的每一行数据
        for (int rowIndex = 0; rowIndex < getdata.size(); rowIndex++) {
            List<String> row = getdata.get(rowIndex);
            String s = row.get(0);//第一行第一列的数据放入集合中
            seriesName.add(s);
            // 遍历当前行中的每一列
            for (int colIndex = 0; colIndex < row.size(); colIndex++) {
                String cellValue = row.get(colIndex);
                System.out.println("  第 " + (colIndex + 1) + " 列: " + cellValue);

            }
        }
        seriesName.remove(0);
        if (configId == 49) {

            Sheet sheet = ExcelUtils.convertDataListToSheetRemoveNineAndOne(chartDateTto.getDataList());
            List<List<String>> getdatas = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdatas)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xList = new ArrayList<>();
            List<String> yList = new ArrayList<>();
            List<Object> pointCuts = new ArrayList<>();
            List<String> sections = getdatas.get(1);
            getdatas.remove(1);

            for (int i = 1; i < sections.size(); i++) {
                String s = sections.get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    pointCuts.add(Double.parseDouble(s));
                }
            }

            List<List<String>> dataList = new ArrayList<>();
            for (int i = 0; i < getdatas.size(); i++) {
                List<String> ss = getdatas.get(i);
                if (!CollectionUtils.isEmpty(ss) && !ss.equals("")) {
                    if (i == 0) {
                        for (int i1 = 1; i1 < ss.size(); i1++) {
                            String y = ss.get(i1);
                            if (!ObjectUtils.isEmpty(y) && !y.equals("")) {
                                yList.add(y);
                            }
                        }
                    } else {
                        String x = ss.get(0);
                        if (!ObjectUtils.isEmpty(x) && !x.equals("")) {
                            xList.add(x);
                        }
                        getdatas.get(i).remove(0);
                        dataList.add(getdatas.get(i));
                    }
                }
            }

            List<ChartXDataVo> xData = new ArrayList<>();
            List<List<Object>> newDataLists = new ArrayList<>();
            for (int i = 0; i < xList.size(); i++) {
                String x = xList.get(i);
                List<List<Object>> newDataList = new ArrayList<>();
                for (int j = 0; j < yList.size(); j++) {
                    String s = dataList.get(i).get(j);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        String y = yList.get(j);
                        List<Object> cellList = new ArrayList<>();
                        cellList.add(x);
                        cellList.add(y);
                        cellList.add(Double.parseDouble(s));

                        List<Object> cellList1 = new ArrayList<>();
                        cellList1.add(x);
                        cellList1.add(y);
                        cellList1.add(Double.parseDouble(s));
                        newDataLists.add(cellList1);
                        newDataList.add(cellList);
                    }
                }
                ChartXDataVo chartXDataVo = new ChartXDataVo();
                chartXDataVo.setSource(newDataList);
                xData.add(chartXDataVo);
            }

            JSONObject jsonObject = JSON.parseObject(chartDateTto.getChartValue());
            jsonObject.put("dataset", xData);

            JSONObject legend = jsonObject.getJSONArray("legend").getJSONObject(0);
            if (legend == null) {
                legend = new JSONObject();
            }

            // 更新 legend 的 data
            JSONArray legendDataArray = new JSONArray();
            for (String xName : xList) {
                JSONObject dataItem = new JSONObject();
                dataItem.put("name", xName);
                legendDataArray.add(dataItem);
            }

            legend.put("data", legendDataArray);
            jsonObject.getJSONArray("legend").set(0, legend);

            JSONArray seriesArray = jsonObject.getJSONArray("series");
            if (seriesArray == null) {
                seriesArray = new JSONArray();
            }

            List<JSONObject> seriesList = new ArrayList<>();
            for (int i = 0; i < xList.size(); i++) {
                JSONObject seriesObject = seriesArray.getJSONObject(0);
                if (seriesObject != null) {
                    JSONObject newSeriesObject = new JSONObject();
                    newSeriesObject.putAll(seriesObject);
                    newSeriesObject.put("datasetIndex", i);
                    newSeriesObject.put("name", xList.get(i));  // 将 name 字段设置为 xList 中的值
                    seriesList.add(newSeriesObject);
                }
            }

            JSONObject seriesObject = seriesArray.getJSONObject(3);
            if (seriesObject != null) {
                for (int i = 0; i < pointCuts.size(); i++) {
                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.putAll(seriesObject);
                    List<List<Object>> list = new ArrayList<>();
                    for (List<Object> objects : newDataLists) {
                        List<Object> objects1 = new ArrayList<>(objects);
                        objects1.set(2, pointCuts.get(i));
                        list.add(objects1);
                    }
                    jsonObject1.put("data", list);
                    seriesList.add(jsonObject1);
                }
            }

            jsonObject.put("series", seriesList);

            JSONObject grid3D = jsonObject.getJSONObject("grid3D");
            if (grid3D == null) {
                grid3D = new JSONObject();
            }

            if (xList.size() > 10 || yList.size() > 10) {
                grid3D.put("boxWidth", xList.size() * 8);
                grid3D.put("boxDepth", yList.size() * 8);
            } else {
                grid3D.put("boxWidth", xList.size() * 20);
                grid3D.put("boxDepth", yList.size() * 20);
            }

            jsonObject.put("grid3D", grid3D);

            // 处理颜色字段
            ObjectMapper objectMapper = new ObjectMapper();
            ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(jsonObject.toJSONString()); // 这里我们解析已经更新过的jsonObject
            JsonNode colorNode = jsonNode.path("color");

            List<String> baseColors = new ArrayList<>();
            if (colorNode.isArray()) {
                for (JsonNode color : colorNode) {
                    String colorValue = color.asText();
                    baseColors.add(colorValue);
                }
            }

            int beforeSize = baseColors.size();
            int afterSize = dataList.size();
            if (beforeSize < afterSize) {
                jsonNode = ColorUtils.processChartColorsHert(jsonNode.toString(), afterSize); // 更新后的jsonNode
            }
            // 更新jsonObject中的颜色字段
            JSONObject updatedJsonObject = JSON.parseObject(jsonNode.toString());

            ArrayList<ChartSmartVo> newList = new ArrayList<>();
            ChartSmartVo chartSmartVo = new ChartSmartVo();
            chartSmartVo.setConfigValue(updatedJsonObject.toJSONString());
            newList.add(chartSmartVo);

            System.out.println("我是configId=49处理之后的" + updatedJsonObject.toJSONString());
            return AjaxResult.success(newList);

        }


        // 更新series
        ObjectNode updatedJson = JsonsUtil.updateSeriesInJson(chartValue, seriesName);

        // 更新 legend 中 data 数组的 name 字段
        updateLegendDataNames(updatedJson, seriesName);

        // 生成并更新 dataset
        ArrayNode datasetArray = JsonsUtil.generateDataset(getdata);
        updatedJson.set("dataset", datasetArray);


        ObjectMapper objectMapper = new ObjectMapper();
        // 处理颜色字段
        ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(chartValue);
        // 获取 color 字段
        JsonNode colorNode = jsonNode.path("color");

        List<String> baseColors = new ArrayList<>();
        // 遍历 color 数组并将每个颜色值添加到 baseColors 集合中
        if (colorNode.isArray()) {
            for (JsonNode color : colorNode) {
                String colorValue = color.asText();
                baseColors.add(colorValue);
            }
        }

        int beforeSize = baseColors.size();
        int afterSize = getdata.size()-1;
        if (beforeSize < afterSize) {
            updatedJson = ColorUtils.processChartColorsHert(JSON.toJSONString(updatedJson), afterSize);
        }

        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigValue(JSON.toJSONString(updatedJson));
        newList.add(chartSmartVo);

        System.out.println(JSON.toJSONString(updatedJson));
        return AjaxResult.success(newList);

    }*/


/*-------------------*/




/*    //多排切面3D柱状图 老代码
    @PostMapping("/multi3DSectionBar/importData")
    public AjaxResult multi3DSectionBar(@RequestBody ChartDateTto chartDateTto) throws Exception {

        String chartValue = chartDateTto.getChartValue();
        Integer configId = chartDateTto.getConfigId();
        List<List<String>> getdata = extractDataFromListNullString(chartDateTto.getDataList());

        if (CollectionUtils.isEmpty(getdata)) {
            return AjaxResult.error("请填充数据");
        }

        List<String> xList = new ArrayList<>();
        List<String> yList = new ArrayList<>();

        List<Object> pointCuts = new ArrayList<>();
        List<String> sections = getdata.get(1);
        getdata.remove(1);
        for (int i = 1; i < sections.size(); i++) {
            String s = sections.get(i);
            if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                pointCuts.add(Double.parseDouble(s));
            }
        }

        List<List<String>> dataList = new ArrayList<>();
        for (int i = 0; i < getdata.size(); i++) {
            List<String> ss = getdata.get(i);
            if (!CollectionUtils.isEmpty(ss) && !ss.equals("")) {
                if (i == 0) {
                    for (int i1 = 1; i1 < ss.size(); i1++) {
                        String y = ss.get(i1);
                        if (!ObjectUtils.isEmpty(y) && !y.equals("")) {
                            yList.add(y);
                        }
                    }
                } else {
                    String x = ss.get(0);
                    if (!ObjectUtils.isEmpty(x) && !x.equals("")) {
                        xList.add(x);
                    }
                    getdata.get(i).remove(0);
                    dataList.add(getdata.get(i));
                }
            }
        }

        List<ChartXDataVo> xData = new ArrayList<>();

        List<List<Object>> newDataLists = new ArrayList<>();
        for (int i = 0; i < yList.size(); i++) {
            String y = yList.get(i);
            List<List<Object>> newDataList = new ArrayList<>();
            for (int j = 0; j < xList.size(); j++) {
                String s = dataList.get(j).get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    String x = xList.get(j);
                    List<Object> cellList = new ArrayList<>();
                    cellList.add(x);
                    cellList.add(y);
                    cellList.add(Double.parseDouble(s));

                    List<Object> cellList1 = new ArrayList<>();
                    cellList1.add(x);
                    cellList1.add(y);
                    cellList1.add(Double.parseDouble(s));
                    newDataLists.add(cellList1);
                    newDataList.add(cellList);
                }
            }
            ChartXDataVo chartXDataVo = new ChartXDataVo();
            chartXDataVo.setSource(newDataList);
            xData.add(chartXDataVo);
        }

        ChartConfigs configs = chartConfigsService.getById(configId);
        JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
        jsonObject.put("dataset", xData);

        // 处理 legend 可能是数组的情况
        Object legendObj = jsonObject.get("legend");
        JSONObject legend = null;
        if (legendObj instanceof JSONArray) {
            JSONArray legendArray = (JSONArray) legendObj;
            if (!legendArray.isEmpty()) {
                legend = (JSONObject) legendArray.get(0); // 假设我们只需要第一个元素
            } else {
                legend = new JSONObject();
            }
        } else if (legendObj instanceof JSONObject) {
            legend = (JSONObject) legendObj;
        } else {
            legend = new JSONObject();
        }

        List<ChartLegendData> chartLegendData = new ArrayList<>();

        List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
        JSONObject o = series.get(0);
        List<JSONObject> seriesList = new ArrayList<>();
        for (int i = 0; i < yList.size(); i++) {
            JSONObject o1 = new JSONObject();
            o1.putAll(o);
            o1.put("datasetIndex", i);
            o1.put("name", yList.get(i));
            seriesList.add(o1);

            ChartLegendData data = new ChartLegendData();
            data.setName(yList.get(i));
            chartLegendData.add(data);
        }

        JSONObject o1 = series.get(3);
        for (int i = 0; i < pointCuts.size(); i++) {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.putAll(o1);
            List<List<Object>> list = new ArrayList<>();
            for (int i1 = 0; i1 < newDataLists.size(); i1++) {
                List<Object> objects = newDataLists.get(i1);
                List<Object> objects1 = new ArrayList<>();
                objects1.addAll(objects);
                objects1.set(2, pointCuts.get(i));
                list.add(objects1);
            }
            jsonObject1.put("data", list);
            seriesList.add(jsonObject1);
        }

        jsonObject.put("series", seriesList);

        legend.put("data", chartLegendData);
        jsonObject.put("legend", legend);

        JSONObject grid3D = (JSONObject) jsonObject.get("grid3D");

        if (xList.size() > 10 || yList.size() > 10) {
            grid3D.put("boxWidth", xList.size() * 8);
            grid3D.put("boxDepth", yList.size() * 8);
        } else if (xList.size() <= 10 && yList.size() <= 10) {
            grid3D.put("boxWidth", xList.size() * 20);
            grid3D.put("boxDepth", yList.size() * 20);
        }

        jsonObject.put("grid3D", grid3D);





        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));
        newList.add(chartSmartVo);

        return AjaxResult.success(newList);
    }*/

/*牛逼66666
    @PostMapping("/multi3DSectionBar/importData")
    public AjaxResult multi3DSectionBar(@RequestBody ChartDateTto chartDateTto) throws Exception {


        Integer configId = chartDateTto.getConfigId();
        List<List<String>> getdata = extractDataFromListNullString(chartDateTto.getDataList());

        if (CollectionUtils.isEmpty(getdata)) {
            return AjaxResult.error("请填充数据");
        }

        List<String> xList = new ArrayList<>();
        List<String> yList = new ArrayList<>();

        List<Object> pointCuts = new ArrayList<>();
        List<String> sections = getdata.get(1);
        getdata.remove(1);
        for (int i = 1; i < sections.size(); i++) {
            String s = sections.get(i);
            if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                pointCuts.add(Double.parseDouble(s));
            }
        }

        List<List<String>> dataList = new ArrayList<>();
        for (int i = 0; i < getdata.size(); i++) {
            List<String> ss = getdata.get(i);
            if (!CollectionUtils.isEmpty(ss) && !ss.equals("")) {
                if (i == 0) {
                    for (int i1 = 1; i1 < ss.size(); i1++) {
                        String y = ss.get(i1);
                        if (!ObjectUtils.isEmpty(y) && !y.equals("")) {
                            yList.add(y);
                        }
                    }
                } else {
                    String x = ss.get(0);
                    if (!ObjectUtils.isEmpty(x) && !x.equals("")) {
                        xList.add(x);
                    }
                    getdata.get(i).remove(0);
                    dataList.add(getdata.get(i));
                }
            }
        }

        List<ChartXDataVo> xData = new ArrayList<>();

        List<List<Object>> newDataLists = new ArrayList<>();
        for (int i = 0; i < yList.size(); i++) {
            String y = yList.get(i);
            List<List<Object>> newDataList = new ArrayList<>();
            for (int j = 0; j < xList.size(); j++) {
                String s = dataList.get(j).get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    String x = xList.get(j);
                    List<Object> cellList = new ArrayList<>();
                    cellList.add(x);
                    cellList.add(y);
                    cellList.add(Double.parseDouble(s));

                    List<Object> cellList1 = new ArrayList<>();
                    cellList1.add(x);
                    cellList1.add(y);
                    cellList1.add(Double.parseDouble(s));
                    newDataLists.add(cellList1);
                    newDataList.add(cellList);
                }
            }
            ChartXDataVo chartXDataVo = new ChartXDataVo();
            chartXDataVo.setSource(newDataList);
            xData.add(chartXDataVo);
        }

        ChartConfigs configs = chartConfigsService.getById(configId);
        JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
        jsonObject.put("dataset", xData);

        // 处理 legend 可能是数组的情况
        Object legendObj = jsonObject.get("legend");
        JSONObject legend = null;
        if (legendObj instanceof JSONArray) {
            JSONArray legendArray = (JSONArray) legendObj;
            if (!legendArray.isEmpty()) {
                legend = (JSONObject) legendArray.get(0); // 假设我们只需要第一个元素
            } else {
                legend = new JSONObject();
            }
        } else if (legendObj instanceof JSONObject) {
            legend = (JSONObject) legendObj;
        } else {
            legend = new JSONObject();
        }

        List<ChartLegendData> chartLegendData = new ArrayList<>();

        List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
        JSONObject o = series.get(0);
        List<JSONObject> seriesList = new ArrayList<>();
        for (int i = 0; i < yList.size(); i++) {
            JSONObject o1 = new JSONObject();
            o1.putAll(o);
            o1.put("datasetIndex", i);
            o1.put("name", yList.get(i));
            seriesList.add(o1);

            ChartLegendData data = new ChartLegendData();
            data.setName(yList.get(i));
            chartLegendData.add(data);
        }

        JSONObject o1 = series.get(3);
        for (int i = 0; i < pointCuts.size(); i++) {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.putAll(o1);
            List<List<Object>> list = new ArrayList<>();
            for (int i1 = 0; i1 < newDataLists.size(); i1++) {
                List<Object> objects = newDataLists.get(i1);
                List<Object> objects1 = new ArrayList<>();
                objects1.addAll(objects);
                objects1.set(2, pointCuts.get(i));
                list.add(objects1);
            }
            jsonObject1.put("data", list);
            seriesList.add(jsonObject1);
        }

        jsonObject.put("series", seriesList);

        legend.put("data", chartLegendData);
        jsonObject.put("legend", legend);

        JSONObject grid3D = (JSONObject) jsonObject.get("grid3D");

        if (xList.size() > 10 || yList.size() > 10) {
            grid3D.put("boxWidth", xList.size() * 8);
            grid3D.put("boxDepth", yList.size() * 8);
        } else if (xList.size() <= 10 && yList.size() <= 10) {
            grid3D.put("boxWidth", xList.size() * 20);
            grid3D.put("boxDepth", yList.size() * 20);
        }

        jsonObject.put("grid3D", grid3D);

        // 新增部分：处理 series 中 type 为 surface 的对象
        series = (List<JSONObject>) jsonObject.get("series");
        List<Integer> removeIndices = new ArrayList<>();
        Integer lastSurfaceIndex = null;
        for (int i = 0; i < series.size(); i++) {
            JSONObject item = series.get(i);
            if ("surface".equals(item.getString("type"))) {
                if (lastSurfaceIndex == null) {
                    lastSurfaceIndex = i;
                } else {
                    removeIndices.add(i);
                }
            }
        }
        for (int i = removeIndices.size() - 1; i >= 0; i--) {
            int indexToRemove = removeIndices.get(i);
            series.remove(indexToRemove);
        }


        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));
        newList.add(chartSmartVo);

        return AjaxResult.success(newList);
    }*/




    /**
     * 可以了 好用的 多排 3D 老代码
     *
     * @param chartDateTto
     * @return
     * @throws Exception
     */
    @PostMapping("/multi3D/importData")
    public AjaxResult multi3DSectionBars(@RequestBody ChartDateTto chartDateTto) throws Exception {
        // 参数校验
        String validationMessage = ChartValidator.check(chartDateTto);
        if (validationMessage != null) {
            // 返回校验失败的提示信息给前端
            return AjaxResult.error(validationMessage);
        }

        String chartValue = chartDateTto.getChartValue();
        Integer configId = chartDateTto.getConfigId();
        List<List<String>> getdata = extractDataFromListNullString(chartDateTto.getDataList());

        // 获取seriesName列表
        ArrayList<String> seriesName = new ArrayList<>();

        //遍历excel的每一行数据
        for (int rowIndex = 0; rowIndex < getdata.size(); rowIndex++) {
            List<String> row = getdata.get(rowIndex);
            String s = row.get(0);//第一行第一列的数据放入集合中
            seriesName.add(s);
            // 遍历当前行中的每一列
            for (int colIndex = 0; colIndex < row.size(); colIndex++) {
                String cellValue = row.get(colIndex);
                System.out.println("  第 " + (colIndex + 1) + " 列: " + cellValue);

            }
        }
        seriesName.remove(0);
        if (configId == 49) {

            Sheet sheet = ExcelUtils.convertDataListToSheetRemoveNineAndOne(chartDateTto.getDataList());
            List<List<String>> getdatas = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdatas)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xList = new ArrayList<>();
            List<String> yList = new ArrayList<>();
            List<Object> pointCuts = new ArrayList<>();
            List<String> sections = getdatas.get(1);
            getdatas.remove(1);

            for (int i = 1; i < sections.size(); i++) {
                String s = sections.get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    pointCuts.add(Double.parseDouble(s));
                }
            }

            List<List<String>> dataList = new ArrayList<>();
            for (int i = 0; i < getdatas.size(); i++) {
                List<String> ss = getdatas.get(i);
                if (!CollectionUtils.isEmpty(ss) && !ss.equals("")) {
                    if (i == 0) {
                        for (int i1 = 1; i1 < ss.size(); i1++) {
                            String y = ss.get(i1);
                            if (!ObjectUtils.isEmpty(y) && !y.equals("")) {
                                yList.add(y);
                            }
                        }
                    } else {
                        String x = ss.get(0);
                        if (!ObjectUtils.isEmpty(x) && !x.equals("")) {
                            xList.add(x);
                        }
                        getdatas.get(i).remove(0);
                        dataList.add(getdatas.get(i));
                    }
                }
            }

            List<ChartXDataVo> xData = new ArrayList<>();
            List<List<Object>> newDataLists = new ArrayList<>();
            for (int i = 0; i < xList.size(); i++) {
                String x = xList.get(i);
                List<List<Object>> newDataList = new ArrayList<>();
                for (int j = 0; j < yList.size(); j++) {
                    String s = dataList.get(i).get(j);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        String y = yList.get(j);
                        List<Object> cellList = new ArrayList<>();
                        cellList.add(x);
                        cellList.add(y);
                        cellList.add(Double.parseDouble(s));

                        List<Object> cellList1 = new ArrayList<>();
                        cellList1.add(x);
                        cellList1.add(y);
                        cellList1.add(Double.parseDouble(s));
                        newDataLists.add(cellList1);
                        newDataList.add(cellList);
                    }
                }
                ChartXDataVo chartXDataVo = new ChartXDataVo();
                chartXDataVo.setSource(newDataList);
                xData.add(chartXDataVo);
            }

            JSONObject jsonObject = JSON.parseObject(chartDateTto.getChartValue());
            jsonObject.put("dataset", xData);

            JSONObject legend = jsonObject.getJSONArray("legend").getJSONObject(0);
            if (legend == null) {
                legend = new JSONObject();
            }

            // 更新 legend 的 data
            JSONArray legendDataArray = new JSONArray();
            for (String xName : xList) {
                JSONObject dataItem = new JSONObject();
                dataItem.put("name", xName);
                legendDataArray.add(dataItem);
            }

            legend.put("data", legendDataArray);
            jsonObject.getJSONArray("legend").set(0, legend);

            JSONArray seriesArray = jsonObject.getJSONArray("series");
            if (seriesArray == null) {
                seriesArray = new JSONArray();
            }

            List<JSONObject> seriesList = new ArrayList<>();
            for (int i = 0; i < xList.size(); i++) {
                JSONObject seriesObject = seriesArray.getJSONObject(0);
                if (seriesObject != null) {
                    JSONObject newSeriesObject = new JSONObject();
                    newSeriesObject.putAll(seriesObject);
                    newSeriesObject.put("datasetIndex", i);
                    newSeriesObject.put("name", xList.get(i));  // 将 name 字段设置为 xList 中的值
                    seriesList.add(newSeriesObject);
                }
            }

            JSONObject seriesObject = seriesArray.getJSONObject(3);
            if (seriesObject != null) {
                for (int i = 0; i < pointCuts.size(); i++) {
                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.putAll(seriesObject);
                    List<List<Object>> list = new ArrayList<>();
                    for (List<Object> objects : newDataLists) {
                        List<Object> objects1 = new ArrayList<>(objects);
                        objects1.set(2, pointCuts.get(i));
                        list.add(objects1);
                    }
                    jsonObject1.put("data", list);
                    seriesList.add(jsonObject1);
                }
            }

            jsonObject.put("series", seriesList);

            JSONObject grid3D = jsonObject.getJSONObject("grid3D");
            if (grid3D == null) {
                grid3D = new JSONObject();
            }

            if (xList.size() > 10 || yList.size() > 10) {
                grid3D.put("boxWidth", xList.size() * 8);
                grid3D.put("boxDepth", yList.size() * 8);
            } else {
                grid3D.put("boxWidth", xList.size() * 20);
                grid3D.put("boxDepth", yList.size() * 20);
            }

            jsonObject.put("grid3D", grid3D);

            // 处理颜色字段
            ObjectMapper objectMapper = new ObjectMapper();
            ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(jsonObject.toJSONString()); // 这里我们解析已经更新过的jsonObject
            JsonNode colorNode = jsonNode.path("color");

            List<String> baseColors = new ArrayList<>();
            if (colorNode.isArray()) {
                for (JsonNode color : colorNode) {
                    String colorValue = color.asText();
                    baseColors.add(colorValue);
                }
            }

            int beforeSize = baseColors.size();
            int afterSize = dataList.size();
            if (beforeSize < afterSize) {
                jsonNode = ColorUtils.processChartColorsHert(jsonNode.toString(), afterSize); // 更新后的jsonNode
            }
            // 更新jsonObject中的颜色字段
            JSONObject updatedJsonObject = JSON.parseObject(jsonNode.toString());

            ArrayList<ChartSmartVo> newList = new ArrayList<>();
            ChartSmartVo chartSmartVo = new ChartSmartVo();
            chartSmartVo.setConfigValue(updatedJsonObject.toJSONString());
            newList.add(chartSmartVo);

            System.out.println("我是configId=49处理之后的" + updatedJsonObject.toJSONString());
            return AjaxResult.success(newList);

        }


        // 更新series
        ObjectNode updatedJson = JsonsUtil.updateSeriesInJson(chartValue, seriesName);

        // 更新 legend 中 data 数组的 name 字段
        updateLegendDataNames(updatedJson, seriesName);

        // 生成并更新 dataset
        ArrayNode datasetArray = JsonsUtil.generateDataset(getdata);
        updatedJson.set("dataset", datasetArray);


        ObjectMapper objectMapper = new ObjectMapper();
        // 处理颜色字段
        ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(chartValue);
        // 获取 color 字段
        JsonNode colorNode = jsonNode.path("color");

        List<String> baseColors = new ArrayList<>();
        // 遍历 color 数组并将每个颜色值添加到 baseColors 集合中
        if (colorNode.isArray()) {
            for (JsonNode color : colorNode) {
                String colorValue = color.asText();
                baseColors.add(colorValue);
            }
        }

        int beforeSize = baseColors.size();
        int afterSize = getdata.size()-1;
        if (beforeSize < afterSize) {
            updatedJson = ColorUtils.processChartColorsHert(JSON.toJSONString(updatedJson), afterSize);
        }

        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigValue(JSON.toJSONString(updatedJson));
        newList.add(chartSmartVo);

        System.out.println(JSON.toJSONString(updatedJson));
        return AjaxResult.success(newList);

    }











    /**
     * 多排 3D切面 新代码
     * @param chartDateTto
     * @return
     * @throws Exception
     */
    @PostMapping("/multi3DSectionBar/importData")
    public AjaxResult multi3DSectionBar(@RequestBody ChartDateTto chartDateTto) throws Exception {

        String chartValue = chartDateTto.getChartValue();
        Integer configId = chartDateTto.getConfigId();
        List<List<String>> getdata = extractDataFromListNullString(chartDateTto.getDataList());




        if (CollectionUtils.isEmpty(getdata)) {
            return AjaxResult.error("请填充数据");
        }

        List<String> xList = new ArrayList<>();
        List<String> yList = new ArrayList<>();


        List<Object> pointCuts = new ArrayList<>();
        List<String> sections = getdata.get(1);
        getdata.remove(1);
        for (int i = 1; i < sections.size(); i++) {
            String s = sections.get(i);
            if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                pointCuts.add(Double.parseDouble(s));
            }
        }

        List<List<String>> dataList = new ArrayList<>();
        for (int i = 0; i < getdata.size(); i++) {
            List<String> ss = getdata.get(i);
            if (!CollectionUtils.isEmpty(ss) && !ss.equals("")) {
                if (i == 0) {
                    for (int i1 = 1; i1 < ss.size(); i1++) {
                        String y = ss.get(i1);
                        if (!ObjectUtils.isEmpty(y) && !y.equals("")) {
                            yList.add(y);
                        }
                    }
                } else {
                    String x = ss.get(0);
                    if (!ObjectUtils.isEmpty(x) && !x.equals("")) {
                        xList.add(x);
                    }
                    getdata.get(i).remove(0);
                    dataList.add(getdata.get(i));
                }
            }
        }

        List<ChartXDataVo> xData = new ArrayList<>();

        List<List<Object>> newDataLists = new ArrayList<>();
        for (int i = 0; i < yList.size(); i++) {
            String y = yList.get(i);
            List<List<Object>> newDataList = new ArrayList<>();
            for (int j = 0; j < xList.size(); j++) {
                String s = dataList.get(j).get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    String x = xList.get(j);
                    List<Object> cellList = new ArrayList<>();
                    cellList.add(x);
                    cellList.add(y);
                    cellList.add(Double.parseDouble(s));

                    List<Object> cellList1 = new ArrayList<>();
                    cellList1.add(x);
                    cellList1.add(y);
                    cellList1.add(Double.parseDouble(s));
                    newDataLists.add(cellList1);
                    newDataList.add(cellList);
                }
            }
            ChartXDataVo chartXDataVo = new ChartXDataVo();
            chartXDataVo.setSource(newDataList);
            xData.add(chartXDataVo);
        }

        ChartConfigs configs = chartConfigsService.getById(configId);
        JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
        jsonObject.put("dataset", xData);

        // 处理 legend 可能是数组的情况
        Object legendObj = jsonObject.get("legend");
        JSONObject legend = null;
        if (legendObj instanceof JSONArray) {
            JSONArray legendArray = (JSONArray) legendObj;
            if (!legendArray.isEmpty()) {
                legend = (JSONObject) legendArray.get(0); // 假设我们只需要第一个元素
            } else {
                legend = new JSONObject();
            }
        } else if (legendObj instanceof JSONObject) {
            legend = (JSONObject) legendObj;
        } else {
            legend = new JSONObject();
        }

        List<ChartLegendData> chartLegendData = new ArrayList<>();

        List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
        JSONObject o = series.get(0);
        List<JSONObject> seriesList = new ArrayList<>();
        for (int i = 0; i < yList.size(); i++) {
            JSONObject o1 = new JSONObject();
            o1.putAll(o);
            o1.put("datasetIndex", i);
            o1.put("name", yList.get(i));
            seriesList.add(o1);

            ChartLegendData data = new ChartLegendData();
            data.setName(yList.get(i));
            chartLegendData.add(data);
        }

        JSONObject o1 = series.get(3);
        for (int i = 0; i < pointCuts.size(); i++) {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.putAll(o1);
            List<List<Object>> list = new ArrayList<>();
            for (int i1 = 0; i1 < newDataLists.size(); i1++) {
                List<Object> objects = newDataLists.get(i1);
                List<Object> objects1 = new ArrayList<>();
                objects1.addAll(objects);
                objects1.set(2, pointCuts.get(i));
                list.add(objects1);
            }
            jsonObject1.put("data", list);
            seriesList.add(jsonObject1);
        }

        jsonObject.put("series", seriesList);

        legend.put("data", chartLegendData);
        jsonObject.put("legend", legend);

        JSONObject grid3D = (JSONObject) jsonObject.get("grid3D");

        if (xList.size() > 10 || yList.size() > 10) {
            grid3D.put("boxWidth", xList.size() * 8);
            grid3D.put("boxDepth", yList.size() * 8);
        } else if (xList.size() <= 10 && yList.size() <= 10) {
            grid3D.put("boxWidth", xList.size() * 20);
            grid3D.put("boxDepth", yList.size() * 20);
        }

        jsonObject.put("grid3D", grid3D);

        // 新增部分：处理 series 中 type 为 surface 的对象
        series = (List<JSONObject>) jsonObject.get("series");
        List<Integer> removeIndices = new ArrayList<>();
        Integer lastSurfaceIndex = null;
        for (int i = 0; i < series.size(); i++) {
            JSONObject item = series.get(i);
            if ("surface".equals(item.getString("type"))) {
                if (lastSurfaceIndex == null) {
                    lastSurfaceIndex = i;
                } else {
                    removeIndices.add(i);
                }
            }
        }
        for (int i = removeIndices.size() - 1; i >= 0; i--) {
            int indexToRemove = removeIndices.get(i);
            series.remove(indexToRemove);
        }


       // 获取 color 字段
        JSONArray colorArray = jsonObject.getJSONArray("color");

        List<String> baseColors = new ArrayList<>();
        // 遍历 color 数组并将每个颜色值添加到 baseColors 集合中
        if (colorArray != null) {
            for (int i = 0; i < colorArray.size(); i++) {
                String colorValue = colorArray.getString(i);
                baseColors.add(colorValue);
            }
        }
        int beforeSize = baseColors.size();
        // 下面是原有代码
        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));
        newList.add(chartSmartVo);

        return AjaxResult.success(newList);
    }








    /*-------------------*/





    //多排3D柱状图
    @PostMapping("/multi3DBar/v2/importData")
    public AjaxResult v2importData(@RequestBody List<Map<String, Object>> dataLists, Long configId) throws Exception {

        XSSFWorkbook workbook = null;
        try {
     /*       workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);*/

            Sheet sheet = ExcelUtils.convertDataListToSheets(dataLists);
            //获取excel读取到的数据
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xList = new ArrayList<>();
            List<String> yList = new ArrayList<>();


            //处理数据
            List<List<String>> dataList = new ArrayList<>();
            for (int i = 0; i < getdata.size(); i++) {
                List<String> ss = getdata.get(i);
                if (!CollectionUtils.isEmpty(ss) && !ss.equals("")) {
                    if (i == 0) {
                        for (int i1 = 1; i1 < ss.size(); i1++) {
                            String y = ss.get(i1);
                            //防止出现为空值或为空字符的数值
                            if (!ObjectUtils.isEmpty(y) && !y.equals("")) {
                                yList.add(y);
                            }
                        }
                    } else {
                        String x = ss.get(0);
                        if (!ObjectUtils.isEmpty(x) && !x.equals("")) {
                            xList.add(x);
                        }
                        getdata.get(i).remove(0);
                        dataList.add(getdata.get(i));
                    }
                }
            }


//            yList.remove(0);

            List<ChartXDataVo> xData = new ArrayList<>();

            for (int i = 0; i < yList.size(); i++) {
                String y = yList.get(i);
                List<List<Object>> newDataList = new ArrayList<>();
                for (int j = 0; j < xList.size(); j++) {
                    String s = dataList.get(j).get(i);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        String x = xList.get(j);
                        List<Object> cellList = new ArrayList<>();
                        cellList.add(x);
                        cellList.add(y);
                        cellList.add(Double.parseDouble(s));
                        newDataList.add(cellList);
                    }
                }
                ChartXDataVo chartXDataVo = new ChartXDataVo();
                chartXDataVo.setSource(newDataList);
                xData.add(chartXDataVo);
            }


            //获取到对应图表的配置，将它反序列化
            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            jsonObject.put("dataset", xData);

            //获取图例配置
            JSONObject legend = (JSONObject) jsonObject.get("legend");
            List<ChartLegendData> chartLegendData = new ArrayList<>();

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject o = series.get(0);
            List<JSONObject> seriesList = new ArrayList<>();
            for (int i = 0; i < yList.size(); i++) {
                JSONObject o1 = new JSONObject();
                o1.putAll(o);
                o1.put("datasetIndex", i);
                o1.put("name", yList.get(i));
                seriesList.add(o1);

                ChartLegendData data = new ChartLegendData();
                data.setName(yList.get(i));
                chartLegendData.add(data);
            }
            jsonObject.put("series", seriesList);

            legend.put("data", chartLegendData);
            jsonObject.put("legend", legend);

            JSONObject grid3D = (JSONObject) jsonObject.get("grid3D");
//            ChartXDataVo chartXDataVo = xData.get(0);
//            int size = chartXDataVo.getSource().size();
//            int i = size * 8;

            //根据数据多少将x,y轴进行按比例放大
            if (xList.size() > 10 || yList.size() > 10) {
                grid3D.put("boxWidth", xList.size() * 8);
                grid3D.put("boxDepth", yList.size() * 8);

            } else if (xList.size() <= 10 && yList.size() <= 10) {
                grid3D.put("boxWidth", xList.size() * 20);
                grid3D.put("boxDepth", yList.size() * 20);
            }

            jsonObject.put("grid3D", grid3D);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
            workbook.close();
        }
    }

    private List<List<String>> extractDataFromListNullString(List<Map<String, Object>> dataList) {
        List<List<String>> extractedData = new ArrayList<>();

        for (Map<String, Object> row : dataList) {
            List<String> rowData = new ArrayList<>();
            for (Map.Entry<String, Object> entry : row.entrySet()) {
                Object value = entry.getValue();
                // 检查值是否为 null 或空字符串，并进行处理
                if (value == null || value.toString().isEmpty()) {
                    rowData.add("0"); // 为空或 null 时添加默认值 "0"
                } else {
                    rowData.add(value.toString());
                }
            }
            extractedData.add(rowData);
        }
        return extractedData;
    }










    //多排切面3D柱状图
    @PostMapping("/multi3DSectionBar/v2/importData")
    public AjaxResult v2multi3DSectionBar(@RequestBody List<Map<String, Object>> dataLists
            , Long configId) throws Exception {
        //       XSSFWorkbook workbook = null;
        try {
 /*           workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);*/

            Sheet sheet = ExcelUtils.convertDataListToSheetRemoveNineAndOne(dataLists);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xList = new ArrayList<>();
            List<String> yList = new ArrayList<>();

            List<Object> pointCuts = new ArrayList<>();
            List<String> sections = getdata.get(1);
            getdata.remove(1);
            for (int i = 1; i < sections.size(); i++) {
                String s = sections.get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    pointCuts.add(Double.parseDouble(s));
                }
            }


            List<List<String>> dataList = new ArrayList<>();
            for (int i = 0; i < getdata.size(); i++) {
                List<String> ss = getdata.get(i);
                if (!CollectionUtils.isEmpty(ss) && !ss.equals("")) {
                    if (i == 0) {
                        for (int i1 = 1; i1 < ss.size(); i1++) {
                            String y = ss.get(i1);
                            if (!ObjectUtils.isEmpty(y) && !y.equals("")) {
                                yList.add(y);
                            }
                        }
                    } else {
                        String x = ss.get(0);
                        if (!ObjectUtils.isEmpty(x) && !x.equals("")) {
                            xList.add(x);
                        }
                        getdata.get(i).remove(0);
                        dataList.add(getdata.get(i));
                    }
                }
            }


//            yList.remove(0);

            List<ChartXDataVo> xData = new ArrayList<>();

            List<List<Object>> newDataLists = new ArrayList<>();
            for (int i = 0; i < yList.size(); i++) {
                String y = yList.get(i);
                List<List<Object>> newDataList = new ArrayList<>();
                for (int j = 0; j < xList.size(); j++) {
                    String s = dataList.get(j).get(i);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        String x = xList.get(j);
                        List<Object> cellList = new ArrayList<>();
                        cellList.add(x);
                        cellList.add(y);
                        cellList.add(Double.parseDouble(s));

                        List<Object> cellList1 = new ArrayList<>();
                        cellList1.add(x);
                        cellList1.add(y);
                        cellList1.add(Double.parseDouble(s));
                        newDataLists.add(cellList1);
                        newDataList.add(cellList);
                    }
                }
                ChartXDataVo chartXDataVo = new ChartXDataVo();
                chartXDataVo.setSource(newDataList);
                xData.add(chartXDataVo);
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            jsonObject.put("dataset", xData);


            JSONObject legend = (JSONObject) jsonObject.get("legend");
            List<ChartLegendData> chartLegendData = new ArrayList<>();

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject o = series.get(0);
            List<JSONObject> seriesList = new ArrayList<>();
            for (int i = 0; i < yList.size(); i++) {
                JSONObject o1 = new JSONObject();
                o1.putAll(o);
                o1.put("datasetIndex", i);
                o1.put("name", yList.get(i));
                seriesList.add(o1);

                ChartLegendData data = new ChartLegendData();
                data.setName(yList.get(i));
                chartLegendData.add(data);
            }


            JSONObject o1 = series.get(3);
            for (int i = 0; i < pointCuts.size(); i++) {
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.putAll(o1);
                List<List<Object>> list = new ArrayList<>();
                for (int i1 = 0; i1 < newDataLists.size(); i1++) {
                    List<Object> objects = newDataLists.get(i1);
                    List<Object> objects1 = new ArrayList<>();
                    objects1.addAll(objects);
                    objects1.set(2, pointCuts.get(i));
                    list.add(objects1);
                }
                jsonObject1.put("data", list);
                seriesList.add(jsonObject1);
            }


            jsonObject.put("series", seriesList);


            legend.put("data", chartLegendData);
            jsonObject.put("legend", legend);

            JSONObject grid3D = (JSONObject) jsonObject.get("grid3D");


            if (xList.size() > 10 || yList.size() > 10) {
                grid3D.put("boxWidth", xList.size() * 8);
                grid3D.put("boxDepth", yList.size() * 8);

            } else if (xList.size() <= 10 && yList.size() <= 10) {
                grid3D.put("boxWidth", xList.size() * 20);
                grid3D.put("boxDepth", yList.size() * 20);
            }


            jsonObject.put("grid3D", grid3D);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
//            workbook.close();
        }
    }
}
